﻿using System;
using System.IO;
using System.Windows.Forms;
using AppUI;
using AppUIView;
using Communication;
using DebuggerApp;
using Global;

namespace TestApp
{
    internal static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MultiFormContext());
        }
    }
    class MultiFormContext : ApplicationContext
    {
 
        public MultiFormContext()
        {
            //初始化自定义 DI 容器
            var container = new DependencyContainer();
            //var assemblyPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AppService.dll");
            //扫描指定程序集，自动注册服务。(需有实现接口的类才能注入依赖)
            var assembly = new string[] { "AppService", "PluginHost" };
            container.RegisterAssemblies(assembly); 
            container.RegisterType<DebuggerMainForm>();

            //解析 DebuggerApp 主窗体（自动注入依赖）
            var mainForm = container.Resolve<DebuggerMainForm>();
            //验证必要服务是否注册
            if (!container._serviceMap.TryGetValue((typeof(ILogger)),out _))
            {
                MessageBox.Show("日志服务未能注册");
                return;
            }
            
            var debugger = container.Resolve<DebuggerMainForm>();
            UIForm1 uIForm1 = new UIForm1();
            ControlSerializer.ApplyControlsFromJson(uIForm1, "test.json");
            debugger.OperationView = (IOperation)uIForm1;
            uIForm1.FormClosed += UIForm1_FormClosed;
            uIForm1.Show();
            debugger.Show();
        }

        private void UIForm1_FormClosed(object sender, FormClosedEventArgs e)
        {
            ControlSerializer.SaveControlsToJson((Control)sender, "test.json");
            // throw new NotImplementedException();
            ExitThread();
        }
    }

}
